package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: bowen
 * @Date: 2019/7/17 15:48
 * @Description:套餐
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

	@Autowired
	JedisPool jedisPool;

	@Reference
	SetmealService setmealService;




	/**
	 * @描述：查询套餐列表
	 */
	@RequestMapping("/getSetmeal")
	public Result findAllSetmeals() {

		try {
			List<Setmeal> setmealList = null;
			if (this.existsKey("setmealList")) {
				setmealList = this.getObject("setmealList", Setmeal.class);
			} else {

				//调用方法从Mysql数据库获取数据
				setmealList = setmealService.findAllSetmeals();
				this.saveObject(setmealList,"setmealList");

			}
			return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, setmealList);

		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
		}

	}

	/**
	 * @描述:根据套餐ID查询套餐详情（1.套餐的基本信息 2.检查组列表  3.检查项信息）
	 */
	@RequestMapping("/findById")
	public Result findById(Integer id) {

		try {
			Setmeal setmeal = null;
			List<Setmeal> setmeal1 = new ArrayList<>();
			if (this.existsKey("setmeal1")) {
				setmeal1 = this.getObject("setmeal1"+String.valueOf(id), Setmeal.class);
				setmeal = setmeal1.get(0);
			} else {

				//调用方法从Mysql数据库获取数据
				setmeal = setmealService.findById(id);
				setmeal1.add(setmeal);
				this.saveObject(setmeal1,"setmeal1"+String.valueOf(id));

			}

			return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
		}
	}




	/**
	 * @Description:判断Redis中是否存在键
	 */
	public boolean existsKey(String key) {
		Jedis jedis = jedisPool.getResource();
		boolean bool;
		try {
			bool = jedis.exists(key);
		} finally {
			jedis.close();
		}
		return bool;
	}

	/**
	 * @Description:取缓存中的二进制数据，反序列化成List集合对象
	 */
	public <T> List<T> getObject(String key, Class<T> clazz) {
		Jedis jedis = jedisPool.getResource();
		// 二进制 IO 输入流
		ByteArrayInputStream is = null;
		ObjectInputStream ois = null;
		try {
			// 从缓存中取二进制数据
			byte[] b = jedis.get(key.getBytes());
			is = new ByteArrayInputStream(b);
			ois = new ObjectInputStream(is);
			// 把二进制转换成T指定类型的集合
			return (List<T>) ois.readObject();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
				ois.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			jedis.close();
		}
		return null;
	}

	/**
	 * @Description:把对象序列化二进制格式并保证到Redis缓存中
	 */
	public void saveObject(Object object, String key) {
		Jedis jedis = jedisPool.getResource();
		// 二进制 IO 输出流
		ByteArrayOutputStream os = null;
		ObjectOutputStream oos = null;
		try {
			os = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(os);
			// 二进制数据
			oos.writeObject(object);
			byte[] b = os.toByteArray();
			// 存入二进制数据到Redis缓存中
			jedis.set(key.getBytes(), b);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				os.close();
				oos.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			jedis.close();
		}

	}

}